let status = 'stoped'
let detailList = {}
let detailStart = false

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(JSON.stringify(request.extra))
    if(request.message === 'status') {
    }
  });

function ajaxDetailCallback(detailItem) {
  console.log(detailItem)
  detailList[detailItem.url] = detailItem
  if (detailStart) {
    setTimeout(function () {
      chrome.tabs.create({url: detailItem.url});
    }, 1000)
  }
}

function ajaxDetail(item) {
  let xhr = new XMLHttpRequest();
  xhr.open("POST", "http://192.168.1.64:3000/ggzy/detail", true);
  // xhr.open("POST", "http://localhost:3000/ggzy/detail", true);
  xhr.setRequestHeader("Content-Type", "application/json");
  let timeout = setTimeout(function () {
    ajaxDetail(item)
  }, 10000)
  xhr.onreadystatechange = function () {
    if(xhr.readyState === 4 && xhr.status === 200) {
      clearTimeout(timeout)
      if (xhr.responseText === 'wait') {
        setTimeout(function () {
          ajaxDetail({})
        }, 600000)
      } else ajaxDetailCallback(JSON.parse(xhr.responseText));
    }
  };
  xhr.send(JSON.stringify(item));
}


